import glob
import os
import re
from bs4 import BeautifulSoup

def find_all_html(paths):
    """
    Find all html files in the given paths.
    """
    return glob.glob("{}/*.html".format(paths))
def filter_html(htmls):
    return list(filter(lambda f: len(re.findall("Builtin",f))>0, htmls))

class HTML_Converer(object):
    """
    Converts a list of HTML files to plain text.
    """

    def __init__(self,htmls):
        self.htmls = htmls

    def convert(self):
        """
        Converts all html files in the list to plain text.
        """
        for html in self.htmls:
            self.convert_one(html)

    def convert_one(self,html):
        """
        Converts a single html file to plain text.
        """
        with open(html,'r') as f:
            soup = BeautifulSoup(open(html))
        sections = soup.find_all('section')
        print(sections[0])

def parse_index(html,content='content.tex'):
    soup = BeautifulSoup(open(html)).find_all('li')
    nodes = [node.string for node in soup if node.string is not None and len(re.findall('3.',node.string))>0]
    with open(content,'w') as f:
        for line in nodes:
            f.write(line+"\n")
    return nodes

def all_bangc_funcs(funcs):
    return [func for func in funcs if len(re.findall(r'(\d+.\){3,5}',func))>0]
if __name__ == '__main__':
    docs_dir = '/home/liushuai/下载/html/2Builtin-Functions'
    htmls = find_all_html(docs_dir)
    need_to_process = filter_html(htmls)
    HtConv = HTML_Converer(need_to_process[1:2])
    #HtConv.convert()
    index = [file for file in htmls if "index" in file]
    print(all_bangc_funcs(parse_index(index[0])))
    
